<?php

namespace Modules\ShopApi\Models;
use Modules\Admin\Models\User as UserModel;
use Illuminate\Support\Facades\DB; 
use Modules\Admin\Services\UserService;  
class ShopUserInfo extends BaseApiModel
{	
	/**
	 * @name  用户登录
	 */
	public function login($data){
		$res = (new UserModel())->login($data);
		if(is_array($res)){
			return $this->api_error($res);
		}else{
			$user = obj_to_arr(DB::table("users")->where('id',$res)->first());	
			if($user){
				$id = $this->where(['user_id'=>$res])->value('id');
				
				if(!$id){
					$this->insert([
						'user_id'=>$res,
						'created_at'=>date('Y-m-d H:i:s'),
						'amount'=>9999,
					]);
				}else{
					$status = $this->where(['id'=>$id])->value('status');
					if($status == 0){
						return $this->api_error(['msg'=>'用户被禁用！']);
					}
				} 
				$token = (new UserService())->set_token(['id'=>$res,'password'=>config('admin.user_password')]);
				if(count($token)){
					return $this->api_success(['data'=>$token]);
				}
			}
			return $this->api_error();
		}
	}
	/**	 
	 * @name  我的信息	 
	 */
	public function my(){
		$user_info = (new UserService())->my();
		if($user_info===false){
			return $this->api_success(['data'=>['status'=>0,'amount'=>'','nick_name'=>'','image_url'=>'','collect_count'=>0,'order_status1'=>0,'order_status2'=>0,'order_status3'=>0,'order_status4'=>0,'order_status5'=>0,'status_num'=>'0','cart_count'=>'0']]);
		}else if(isset($user_info['code']) && $user_info['code'] == 1003){
			return $this->api_error($user_info);
		}
		$arr = [];
		if($user_info['image_id']){
			$image = obj_to_arr(DB::table("images")->where('id',$user_info['image_id'])->first());
			if($image['open'] == 1){
				$arr['image_url'] = getHttpType() . $image['url'];
			}else{
				$arr['image_url'] = $image['url'];
			}
		}else{
			$arr['image_url'] = $user_info['avatar_url'];
		}
		$arr['collect_count'] = DB::table('shop_collects')->where(['user_id'=>$user_info['id'],'status'=>0])->count();
		$arr['order_status1'] = 0;
		$arr['order_status2'] = 0;
		$arr['order_status3'] = 0;
		$arr['order_status4'] = 0;
		$arr['order_status5'] = 0;
		$order_status = DB::table('shop_orders')->where(['user_id'=>$user_info['id']])->whereIn('status',[1,2,3,4,5,6,7,8])->select('id','status')->get();
		foreach($order_status as $k=>$v){
			if($v->status == 1){
				$arr['order_status1']+=1;
			}else if($v->status == 2){
				$arr['order_status2']+=1;
			}else if($v->status == 3){
				$arr['order_status3']+=1;
			}else if($v->status == 4){
				$arr['order_status4']+=1;
			}else if(in_array($v->status,[5,6,7,8])){
				$arr['order_status5']+=1;
			}
		}
		$arr['status_num'] = $arr['collect_count'] + count($order_status);
		$arr['status_num'] = (string)$arr['status_num'];
		$arr['cart_count'] = (string)DB::table('shop_carts')->where(['user_id'=>$user_info['id']])->count();
		$arr['amount'] = $this->where(['user_id'=>$user_info['id']])->value('amount');
		$arr['nick_name'] = $user_info['nick_name'];
		$arr['status'] = 1;
		return $this->api_success(['data'=>$arr]);
	}
	/**	 
	 * @name  退出登录
	 */
    public function logout()
    {
		(new UserService())->logout();
        return $this->api_success(['msg'=>'退出成功！']);
    }
	/**	 
	 * @name  刷新	 
	 */
    public function refresh()
    {
		$token = (new UserService())->refresh();
        return $this->api_success(['data'=>$token]);
    }
	
	/**
     * @name  修改头像 
     */
	public function edit_avatar($id){
		$user_info = (new UserService())->my();
		DB::beginTransaction();
		try{
			DB::table('users')->where(['id'=>$user_info['id']])->update([
					'updated_at'=>date('Y-m-d H:i:s'),
					'image_id'=>$id
			]);
			DB::table('images')->where(['id'=>$id])->update([
					'updated_at'=>date('Y-m-d H:i:s'),
					'status'=>1
			]);
		DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();		
	}
	/**
     * @name  修改昵称 
     */
	public function edit_nick_name($nick_name){
		$user_info = (new UserService())->my();
		$res = DB::table('users')->where(['id'=>$user_info['id']])->update([
					'updated_at'=>date('Y-m-d H:i:s'),
					'nick_name'=>$nick_name
			]);
		if($res !== false){
			return $this->api_success();
		}else{
			return $this->api_error();
		}		
	}
}
